import React,{useEffect,useState} from 'react';
import { Button, Checkbox, Form, Input, Select, Switch,message } from 'antd';
import './regulation.scss'
import  * as service from '../../../api/cyyindex'
interface Props {

}
function Regulation(props: Props) {
  const [messageApi, contextHolder] = message.useMessage();
  const [myFlag, setMyflag] = useState(false)
  const [form] = Form.useForm();
  useEffect(() => {
    service.personnel_list().then(res => {
      console.log(res)
      if (res.data) {
        service.personnel_info({ id: 2 }).then(res => {
          console.log(res)
          form.setFieldsValue(res.data)

        })
        setMyflag(true)
      }
    })
    
  },[])
  const onFinish = (values: any) => {
    console.log('Success:', values);
    if (myFlag) {
      values.id=2
      service.personnel_edit( values ).then(res => {
        console.log(res)
        if (res.status == 200) {
          messageApi.open({
            type: 'success',
            content: '修改成功',
          });
        }
      })
    } else {
      service.personnel_add(values).then(res => {
        console.log(res)
        if (res.status == 200) {
          messageApi.open({
            type: 'success',
            content: '新增成功',
          });
        }
      })
    }
   
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log('Failed:', errorInfo);
  };
  return (
    <div className='regulation'>
      <p className='title'>人事规则设置</p>
      <div className='box'>
        {contextHolder}
        <Form
          form={form}
        name="basic"
        labelCol={{ span: 8 }}
        wrapperCol={{ span: 16 }}
        style={{ maxWidth: 600 }}
        initialValues={{ remember: true }}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
        autoComplete="off"
      >
        <Form.Item
   valuePropName="checked"
          name="entry"
          wrapperCol={{ offset: 8, span: 16 }}
        >
          <Checkbox >员工入职年龄限制</Checkbox>
        </Form.Item>

        <Form.Item
          label="年龄范围"
          name="age"
          rules={[{ required: true, message: 'Please input your username!' }]}
        >
         
          <Input  />
       
          
        </Form.Item>


        <Form.Item
        valuePropName="checked"
          name="flag"
          wrapperCol={{ offset: 8, span: 16 }}
        >
          <Checkbox >允许离职员工重新入职</Checkbox>
        </Form.Item>
        <Form.Item
          label="转正规则"
          name="rules"
        >
          <Select
            defaultValue="请选择"
            options={[
              { value: '试用期满当天转正', label: '试用期满当天转正' },
              { value: '试用期满前一天转正', label: '试用期满当天转正' },
              { value: '试用期满次日转正', label: '试用期满次日转正' },

            ]}
          />
        </Form.Item>
        <Form.Item
          label="男性职工退休年龄"
          name="old1"
        >
         <Input />
         
        </Form.Item>
        <Form.Item
          label="女性职工退休年龄"
          name="old"
        >
        
       <Input />
        </Form.Item>
        <Form.Item
          label="人事变动提醒"
          name="remind"
        >
          <Switch defaultChecked />
        </Form.Item>
        <Form.Item
          label="提醒时间"
            name="date"
            
        >
          <Select
            defaultValue="请选择"
            options={[
              { value: '不提醒', label: '不提醒' },
              { value: '提前1天', label: '提前1天' },
              { value: '提前3天', label: '提前3天' },
              { value: '提前5天', label: '提前5天', },
              { value: '提前15天', label: '提前15天', },
              { value: '提前30天', label: '提前30天', },
            ]}
          />
        </Form.Item>



        <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
         
            {
              myFlag? <Button type="primary" htmlType="submit">
              修改
              </Button>: <Button type="primary" htmlType="submit">
            保存
            </Button>
            }
        </Form.Item>
        </Form>
        </div>
    </div>
  );
}

export default Regulation;